﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.ToolWindows
{
	/// <summary>
	/// 
	/// </summary>
	public partial class WoWBenchControl : UserControl, IToolWindowControl
	{
		#region Private Variables

		private readonly Pen pen = new Pen(Color.DarkGray);
		private readonly IWoWBenchController woWBenchController;

		#endregion

		#region Lifecycle Methods

		/// <summary>
		/// Initializes a new instance of the <see cref="WoWBenchControl"/> class.
		/// </summary>
		/// <param name="controller">The WoWBenchController.</param>
		public WoWBenchControl(IWoWBenchController controller)
		{
			if (controller == null)
				throw new ArgumentNullException("controller");

			woWBenchController = controller;

			InitializeComponent();

			//Set image for controls
			btnRunWoWBenchEmulator.Image = Resources.Launch;
			btnExecuteAddOnCommand.Image = Resources.Servicerunning.ToBitmap();
			cutToolStripMenuItem.Image = Resources.Cut;
			copyToolStripMenuItem.Image = Resources.Copy;
			pasteToolStripMenuItem.Image = Resources.Paste;
			clearAllToolStripMenuItem.Image = Resources.DeleteTable;
			commandHistoryDropDown.Image = Resources.Legend;
			runLuaToolStripMenuItem.Image = Resources.lua_icon;
			runWoWBenchToolStripMenuItem.Image = Resources.basic_wow_16x16.ToBitmap();

			StartAddOnMenuItem.Image = btnExecuteAddOnCommand.Image;
			StartLuaMenuItem.Image = runLuaToolStripMenuItem.Image;
			StartWoWBenchMenuItem.Image = runWoWBenchToolStripMenuItem.Image;

			WoWBenchController.Initialize(txtOutput);

			//Initialize CommandHistory DropDown control
			commandHistoryDropDown.Initialize(WoWBenchController.CommandHistory);
			commandHistoryDropDown.HistoryItemSelected += OnHistoryItemSelected;

			ReadOnly = true;
		}


		#endregion

		#region Protected Members

		/// <summary>
		/// Executes Lua command in controller.
		/// </summary>
		protected virtual string ExecuteLuaCommand()
		{
			return WoWBenchController.WowBenchCommand.Execute();
		}

		/// <summary>
		/// Executes Lua command in controller.
		/// </summary>
		protected virtual string ExecuteLuaCommandOnDisplay()
		{
			string command = ExecuteLuaCommand();

			ColorizeText(txtOutput.Lines.Length - 1, command, Color.Blue);
			txtOutput.AppendText(Environment.NewLine);

			return command;
		}

		/// <summary>
		/// Gets the WoWBenchController.
		/// </summary>
		protected IWoWBenchController WoWBenchController
		{
			get { return woWBenchController; }
		}

		#endregion

		#region IToolWindowControl Members

		/// <summary>
		/// Indicates that the Control is readonly.
		/// </summary>
		public bool ReadOnly { get; set; }


		/// <summary>
		/// Gets the output control.
		/// </summary>
		public Control OutputControl
		{
			get { return txtOutput; }
		}

		/// <summary>
		/// Sets the state of controls.
		/// </summary>
		/// <param name="enabled"></param>
		public virtual void SetControlState(bool enabled)
		{
			btnExecuteAddOnCommand.Enabled = enabled;
			StartAddOnMenuItem.Enabled = enabled;
			ReadOnly = !enabled;
		}

		#endregion

		#region User Interface Event Handlers

		/// <summary>
		/// Load current AddOn into WoWBench.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnExecuteAddOnCommandClick(object sender, EventArgs e)
		{
			WoWBenchController.Initialize(txtOutput);
			bool runResult = woWBenchController.RunWoWBenchAddOn();
			SetControlState(runResult);
			commandHistoryDropDown.Reset();
		}


		/// <summary>
		/// Runs the wo W bench tool strip menu item click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void runWoWBenchToolStripMenuItemClick(object sender, EventArgs e)
		{
			WoWBenchController.Initialize(txtOutput);
			bool runResult = WoWBenchController.RunWoWBench();
			SetControlState(runResult);
			commandHistoryDropDown.Reset();
		}


		/// <summary>
		/// Runs the lua tool strip menu item click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void runLuaToolStripMenuItemClick(object sender, EventArgs e)
		{
			WoWBenchController.Initialize(txtOutput);
			bool runResult = WoWBenchController.RunLuaInterpreter();
			SetControlState(runResult);
			commandHistoryDropDown.Reset();
		}

		#endregion

		#region Context Menu handlers, ClipBoard functions


		/// <summary>
		/// Called when [history item selected].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="Microsoft.WowAddonStudio.Lua.Runtime.ToolWindows.HistoryItemSelectedEventArgs"/> instance containing the event data.</param>
		private void OnHistoryItemSelected(object sender, HistoryItemSelectedEventArgs e)
		{
			SetText(e.Command);
			ExecuteLuaCommandOnDisplay();
		}


		/// <summary>
		/// Handles the Click event of the cutToolStripMenuItem control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void cutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			txtOutput.Cut();
		}


		/// <summary>
		/// Handles the Click event of the copyToolStripMenuItem control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void copyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			txtOutput.Copy();
		}


		/// <summary>
		/// Handles the Click event of the pasteToolStripMenuItem control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (Clipboard.ContainsText())
			{
				SetInputLine();
				//Prevent inserting rich text from clipboard.
				txtOutput.SelectedText = Clipboard.GetText();
				HandleOutputKeyPress(string.Empty);
			}
		}


		/// <summary>
		/// Handles the Click event of the clearAllToolStripMenuItem control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			txtOutput.Clear();
		}

		#endregion
	}
}